import { ref, watch, toRaw } from "vue";
import { actList } from "../../../../api/common";

const actDataOptions = [{ label: "全部活动", value: "" }];
interface IOptions {
  label: string;
  value: string;
}

interface FetchData {
  (): void;
}

export default function useActListData(formState: any, next?: FetchData) {
  const activitiesData = ref<Array<IOptions>>(actDataOptions);
  const getActList = async () => {
    let data = toRaw(formState);
    let res = await actList({
      appletCode: data.applet
    });
    if (res.code === 0) {
      let list = res.data.map((item: any) => {
        const obj = {
          label: item.name,
          value: item.actId
        };
        return obj;
      });
      activitiesData.value = actDataOptions.concat(list);
      console.log("activitiesData.value", activitiesData.value);
    }
    next && next();
  };
  //getActList();
  watch(formState, () => {
    //有小程序code的时候，请求活动下拉数据
    if (formState.applet) {
      getActList();
    }
  });
  return {
    activitiesData
  };
}
